const fs = require('fs')
const mongoose = require('mongoose')

// 连接数据库
// mongoose.connect('mongodb://localhost:27017/supermarketCase', {useNewUrlParser: true})

DB_URL = 'mongodb://localhost:27017/supermarketCase'; /** * 连接 */ 
mongoose.connect(DB_URL, {useNewUrlParser: true, useUnifiedTopology: true }); 
mongoose.connection.on('connected', function () { 
console.log('连接成功:Mongoose connection open to ' + DB_URL); }); /** * 连接成功 */ 

mongoose.connection.on('error',function (err) { 
console.log('连接异常：Mongoose connection error: ' + err); });/** * 连接异常 */ 


mongoose.connection.on('disconnected', function () { 
console.log('连接断开：Mongoose connection disconnected'); }); /** * 连接断开 */ 

let Schema = mongoose.Schema


// 登录账户表
let userSchema = new Schema({
    id: {
        type: Number,
        default: 0
    },
    userCode: {
        type: String,
        required: true
    },
    userName: {
        type: String,
        required: true
    },
    userPassword: {
        type: String,
        required: true
    },
    gender: {
        type: Number,
        required: true,
    },
    birthday: {
        type: String
    },
    phone: {
        type: String,
        required: true
    },
    address: {
        type: String
    },
    creationDate: {
        type: Date
    },
    modifyDate: {
        type: Date
    },
    role: {
        type: String,
        default: '普通员工'
    }
})

// 供应商表
let providerSchema = new Schema({
    providerCode: {
        type: String,
        required: true
    },
    providerName: {
        type: String,
        required: true
    },
    linkman: {
        type: String,
        required: true
    },
    phone: {
        type: String,
        required: true
    },
    fax: {
        type: String
    },
    address: {
        type: String
    },
    createDate: {
        type: String
    },
    desc: {
        type: String
    }
})

// 角色表
let roleSchema = new Schema({
    roleId: {
        type: Number,
        required: true
    },
    roleName: {
        type: String,
        required: true
    }
})

// 订单表
let orderSchema = new Schema({
    orderCode: {    // 订单编码
        type: String,
        required: true,
        unique: true
    },
    orderName: {    // 订单名称
        type:String,
        required: true
    },  
    providerName: {     // 供应商名称
        type: String,
        required: true
    },
    commUnit:{      // 商品单位
        type: String
    },
    orderAmount: {  // 订单金额
        type: String,
        required: true
    },
    commNum: {
        type: Number,
        default: 1
    },
    isPay: {    // 是否付款
        type: String,
        required: true
    },  
    createDate: {   // 创建时间
        type: String,
        required: true
    }
})

// 商品表
let commSchema = new Schema({
    commCode: {
        type: String,
        required: true,
        unique: true
    },
    commName: {
        type: String,
        required: true
    },
    commUnit: {
        type: String,
        required: true
    },
    commPrice: {
        type: String,
        required: require
    },
    commNum: {
        type: String,
        required: require
    }
})


module.exports = {
    users: mongoose.model('users', userSchema),
    providers: mongoose.model('providers', providerSchema),
    roles: mongoose.model('roles', roleSchema),
    orders: mongoose.model('orders', orderSchema),
    comms: mongoose.model('comms', commSchema)
}